﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.Inventory.BL;


namespace com.vs.Receiving.BL
{
    public class POManager
    {
        public string SavePODetail(Session sess, PurchaseOrderDetailDO poDtlDO)
        {
            int iBaseQty = 0;
            string stBaseUM = null;

            try
            {
                //如果客户在前端录入的不是基本数量单位，在这里我们要做转换
                stBaseUM = ItemManager.GetBaseUM(sess, poDtlDO.ITEM, poDtlDO.COMPANY);
                if (!StringManager.Equals(stBaseUM, poDtlDO.QUANTITY_UM))
                {
                    poDtlDO.QUANTITY_UM = stBaseUM;

                    //1. 计算总数量
                    iBaseQty = ItemManager.GetBaseQty(sess, poDtlDO.ITEM, poDtlDO.COMPANY, poDtlDO.TOTAL_QTY, poDtlDO.QUANTITY_UM);
                    poDtlDO.TOTAL_QTY = iBaseQty;

                    //2. 计算待收数量
                    poDtlDO.OPEN_QTY = ItemManager.GetBaseQty(sess, poDtlDO.ITEM, poDtlDO.COMPANY, poDtlDO.OPEN_QTY, poDtlDO.QUANTITY_UM);
                }
                
                if (poDtlDO.INTERNAL_PURCHASE_ORDER_LINE_NUM > 0)
                    return poDtlDO.Update(sess);
                else
                {
                    return "" + poDtlDO.Insert(sess);
                }
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        public string GenerateReceiptHeader(Session sess, int iIntPONum, string stReceiptID)
        {
            ReceiptHeaderDO rcpHdrDO = null;
            PurchaseOrderHeaderDO poHdrDO = null;

            try
            {
                poHdrDO = PurchaseOrderHeaderDO.GetModelByPK(sess, iIntPONum);
                if (poHdrDO == null)
                    return MessageManager.MSG_RCP_019;//错误：采购单不存在

                rcpHdrDO = new ReceiptHeaderDO();
                rcpHdrDO.COMPANY = poHdrDO.COMPANY;
                rcpHdrDO.DATE_TIME_STAMP = System.DateTime.Now;
                rcpHdrDO.ERP_ORDER_ID = poHdrDO.ERP_ORDER_ID;
                rcpHdrDO.LEADING_STS = (int)Constants.InboundStatus.iCHECK_IN_PENDING;
                rcpHdrDO.TRAILING_STS = (int)Constants.InboundStatus.iCHECK_IN_PENDING;
                rcpHdrDO.PRIORITY = poHdrDO.PRIORITY;
                rcpHdrDO.RECEIPT_ID = stReceiptID;
                rcpHdrDO.RECEIPT_TYPE = poHdrDO.PURCHASE_ORDER_TYPE;
                rcpHdrDO.RECV_DOCK = "RECV-01";//TODO:update to default recv dock
                rcpHdrDO.SCHEDULED_ARRIVE_DATE = DateTime.Today.AddDays(10);//todo:default arrival date.
                rcpHdrDO.SHIP_FROM = poHdrDO.SHIP_FROM;
                rcpHdrDO.SHIP_FROM_ADDRESS1 = poHdrDO.SHIP_FROM_ADDRESS1;
                rcpHdrDO.SHIP_FROM_ADDRESS2 = poHdrDO.SHIP_FROM_ADDRESS2;
                rcpHdrDO.SHIP_FROM_ATTENTION_TO = poHdrDO.SHIP_FROM_ATTENTION_TO;
                rcpHdrDO.SHIP_FROM_CITY = poHdrDO.SHIP_FROM_CITY;
                rcpHdrDO.SHIP_FROM_COUNTRY = poHdrDO.SHIP_FROM_COUNTRY;
                rcpHdrDO.SHIP_FROM_EMAIL_ADDRESS = poHdrDO.SHIP_FROM_EMAIL_ADDRESS;
                rcpHdrDO.SHIP_FROM_FAX_NUM = poHdrDO.SHIP_FROM_FAX_NUM;
                rcpHdrDO.SHIP_FROM_NAME = poHdrDO.SHIP_FROM_NAME;
                rcpHdrDO.SHIP_FROM_PHONE_NUM = poHdrDO.SHIP_FROM_PHONE_NUM;
                rcpHdrDO.SHIP_FROM_POSTAL_CODE = poHdrDO.SHIP_FROM_POSTAL_CODE;
                rcpHdrDO.SHIP_FROM_STATE = poHdrDO.SHIP_FROM_STATE;
                rcpHdrDO.USER_STAMP = sess.User;
                rcpHdrDO.WAREHOUSE = poHdrDO.WAREHOUSE;

                return "" + rcpHdrDO.Insert(sess);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        public string GenerateReceiptDetail(Session sess, int iPOLineNum, string stReceiptID, int iIntReceiptNum, int iQty)
        {
            PurchaseOrderDetailDO pdDO = null;
            ReceiptDetailDO rdDO = null;
            string stErr = null;
            Dictionary<string, object> dcValue = null;

            try
            {
                pdDO = PurchaseOrderDetailDO.GetModelByPK(sess, iPOLineNum);
                if (pdDO == null)
                    return MessageManager.MSG_RCP_020; //错误：采购单明细不存在

                rdDO = new ReceiptDetailDO();
                rdDO.ATTRIBUTE_TRACK = pdDO.ATTRIBUTE_TRACK;
                rdDO.COMPANY = pdDO.COMPANY;
                rdDO.DATE_TIME_STAMP = DateTime.Now;
                rdDO.ERP_ORDER_ID = pdDO.PURCHASE_ORDER_ID;
                rdDO.ERP_ORDER_LINE_NUM = pdDO.ERP_ORDER_LINE_NUM;
                //rdDO.INTERNAL_RECEIPT_LINE_NUM = pdDO
                rdDO.INTERNAL_RECEIPT_NUM = iIntReceiptNum;
                rdDO.INVENTORY_STS = pdDO.INVENTORY_STS;
                rdDO.ITEM = pdDO.ITEM;
                rdDO.ITEM_CATEGORY1 = pdDO.ITEM_CATEGORY1;
                rdDO.ITEM_CATEGORY2 = pdDO.ITEM_CATEGORY2;
                rdDO.ITEM_CATEGORY3 = pdDO.ITEM_CATEGORY3;
                rdDO.ITEM_CATEGORY4 = pdDO.ITEM_CATEGORY4;
                rdDO.ITEM_CATEGORY5 = pdDO.ITEM_CATEGORY5;
                rdDO.ITEM_CATEGORY6 = pdDO.ITEM_CATEGORY6;
                rdDO.ITEM_CATEGORY7 = pdDO.ITEM_CATEGORY7;
                rdDO.ITEM_CATEGORY8 = pdDO.ITEM_CATEGORY8;
                rdDO.ITEM_CLASS = pdDO.ITEM_CLASS;
                rdDO.ITEM_DESC = pdDO.ITEM_DESC;
                rdDO.OPEN_QTY = iQty;
                rdDO.QUANTITY_UM = pdDO.QUANTITY_UM;
                rdDO.RECEIPT_ID = stReceiptID;
                rdDO.TOTAL_QTY = iQty;
                rdDO.USER_STAMP = sess.User;
                rdDO.WAREHOUSE = pdDO.WAREHOUSE;

                stErr = rdDO.Insert(sess);
                if (!StringManager.IsErrorMessage(stErr))
                {
                    dcValue = new Dictionary<string,object>();
                    dcValue.Add("OPEN_QTY", pdDO.OPEN_QTY - iQty);
                    pdDO.Update(sess, dcValue);
                }
                return stErr;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000;
            }
            finally
            {
            }
        }

        public string ClosePO(Session sess, int iPONum)
        {
            PurchaseOrderHeaderDO phDO = null;
            Dictionary<string, object> dcValue = null;

            try
            {
                phDO = PurchaseOrderHeaderDO.GetModelByPK(sess, iPONum);
                if (null == phDO)
                    return MessageManager.MSG_RCP_019;

                if (phDO.STATUS == (int)Constants.InboundStatus.iCLOSED)
                    return null;

                dcValue = new Dictionary<string, object>();
                dcValue.Add("STATUS", 900);
                phDO.Update(sess, dcValue);
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        public string DeletePODetail(Session sess, int iPOLineNum)
        {
            PurchaseOrderDetailDO pdDO = null;

            try
            {
                pdDO = PurchaseOrderDetailDO.GetModelByPK(sess, iPOLineNum);
                if (pdDO == null)
                    return MessageManager.MSG_RCP_020;

                pdDO.Delete(sess);
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        public string DeletePO(Session sess, int iPONum)
        {
            PurchaseOrderHeaderDO phDO = null;

            try
            {
                phDO = PurchaseOrderHeaderDO.GetModelByPK(sess, iPONum);
                if (phDO == null)
                    return MessageManager.MSG_RCP_019;

                phDO.Delete(sess);
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        public DataTable GetPOHdrsByQFCri(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy, ref string stError)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                sbSQL = new StringBuilder(" SELECT DISTINCT PH.* FROM PURCHASE_ORDER_HEADER PH LEFT JOIN PURCHASE_ORDER_DETAIL PD ON PH.INTERNAL_PURCHASE_ORDER_NUM = PD.INTERNAL_PURCHASE_ORDER_NUM WHERE 1=1 ");
                helper = new DataHelper(sess);
                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key.ToString()).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                stError = MessageManager.MSG_GEN_000 + e.Message;
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

    }
}
